package com.weic.easycache.helper;

import java.lang.ref.WeakReference;
import java.util.HashMap;
import java.util.Map;

import android.content.Context;

import com.weic.easycache.framework.Errors;
import com.weic.easycache.framework.Result;
import com.weic.easycache.utils.IdGetter;

public class MsgHelper {
	public static String resolveMessage(Context context, int status, String message, int errorType, int errorCode) {
		switch (status) {
		case Result.STATUS_COMPLETE:
		case Result.STATUS_UPDATE:
			return message;
		case Result.STATUS_WAITING:
		case Result.STATUS_LOADING:
			if(message == null || message.length() == 0) message = getDefaultMsg(context, status, Errors.NO);
			return message;
		case Result.STATUS_PROGRESS:  //非错误状态下，为空才加载默认
			if(errorType == Errors.NO && message != null && message.length() > 0) return message;
			break;
		case Result.STATUS_FAILURE:
			break;
		}
		String msg;
		if(errorType != Errors.NO) {
			msg = getDefaultMsg(context, errorType, errorCode);
		}else {
			msg = getDefaultMsg(context, status, Errors.NO);
		}
		if(msg != null && msg.length() > 0) message = msg;
		return message;
	}

	private static String getDefaultMsg(Context context, int type, int errorCode) {
		String idName = null;
		boolean addCode = false;
		switch (type) {
		case Result.STATUS_WAITING:
			idName = ID_STR_STATUS_WAITING;
			break;
		case Result.STATUS_LOADING:
			idName = ID_STR_STATUS_LOADING;
			break;
		case Result.STATUS_PROGRESS:
			idName = ID_STR_STATUS_PROGRESS;
			break;
		case Result.STATUS_FAILURE:
			idName = ID_STR_STATUS_FAILURE;
			break;
		case Errors.UNKNOWN:
			idName = ID_STR_ERROR_UNKNOWN;
			addCode = true;
			break;
		case Errors.NET_NO_HOST:
			idName = ID_STR_ERROR_NET_NO_HOST;
			break;
		case Errors.NET_TIMEOUT:
			idName = ID_STR_ERROR_NET_TIMEOUT;
			break;
		case Errors.HTTP_STATUS:
			idName = ID_STR_ERROR_HTTP_STATUS;
			addCode = true;
			break;
		case Errors.API_STATUS:
			idName = ID_STR_ERROR_API_STATUS;
			break;
		case Errors.API_ERROR:
			idName = ID_STR_ERROR_API_ERROR;
			addCode = true;
			break;
		case Errors.PARAM_LACKED:
			idName = ID_STR_ERROR_PARAM_LACKED;
			break;
		case Errors.NO_CACHE:
			idName = ID_STR_ERROR_NO_CACHE;
			break;
		case Errors.NO_DATA:
			idName = ID_STR_ERROR_NO_DATA;
			break;
		case Errors.NEED_NET:
			idName = ID_STR_ERROR_NEED_NET;
			break;
		case Errors.NEED_LOGIN:
			idName = ID_STR_ERROR_NEED_LOGIN;
			break;
		default:
			idName = ID_STR_ERROR_USER + TYPE + type;
			addCode = true;
			break;
		}
		String msg = null;
		int id = 0;
		String s = idName;
		idName = errorCode != Errors.NO ? idName + CODE + errorCode : idName;
		msg = getCache(idName);
		if(msg != null) return msg;
		id = IdGetter.getId(context, idName, IdGetter.string, false);
		if(id == 0 && idName != s) {
			idName = s;
			msg = getCache(idName);
			if(msg != null) return msg;
			id = IdGetter.getId(context, idName, IdGetter.string, false);
		}
		if(id != 0) {
			msg = context.getString(id);
			if(addCode && msg != null && msg.length() > 0) {msg += "(" + type + ", " + errorCode + ")";}
			if(msg != null) cache(idName, msg);		//即使为空字符，也要cache
		}
		return msg;
	}
	private static String TYPE = "_type_";
	private static String CODE = "_code_";

	private static Map<String, WeakReference<String>> msgCache = new HashMap<String, WeakReference<String>>();
	private static String getCache(String key) {
		String result = null;
		WeakReference<String> ref = msgCache.get(key);
		if(ref != null) result = ref.get();
		return result;
	}

	private static void cache(String key, String value) {
		msgCache.put(key, new WeakReference<String>(value));
	}

	public static final String ID_STR_STATUS_WAITING				= "lib_wei_c_msg_status_waiting";
	public static final String ID_STR_STATUS_LOADING				= "lib_wei_c_msg_status_loading";
	public static final String ID_STR_STATUS_PROGRESS				= "lib_wei_c_msg_status_progress";
	public static final String ID_STR_STATUS_FAILURE				= "lib_wei_c_msg_status_failure";
	public static final String ID_STR_ERROR_UNKNOWN					= "lib_wei_c_msg_error_unknown";
	public static final String ID_STR_ERROR_NET_NO_HOST				= "lib_wei_c_msg_error_net_no_host";
	public static final String ID_STR_ERROR_NET_TIMEOUT				= "lib_wei_c_msg_error_net_timeout";
	/**http错误代码配置。如：lib_wei_c_msg_error_http_status_code_456**/
	public static final String ID_STR_ERROR_HTTP_STATUS				= "lib_wei_c_msg_error_http_status";
	/**网校接口Api返回status字段错误代码配置。如：lib_wei_c_msg_error_api_status_code_456**/
	public static final String ID_STR_ERROR_API_STATUS				= "lib_wei_c_msg_error_api_status";
	public static final String ID_STR_ERROR_API_ERROR				= "lib_wei_c_msg_error_api_error";
	public static final String ID_STR_ERROR_PARAM_LACKED			= "lib_wei_c_msg_error_param_lacked";
	public static final String ID_STR_ERROR_NO_CACHE				= "lib_wei_c_msg_error_no_cache";
	public static final String ID_STR_ERROR_NO_DATA					= "lib_wei_c_msg_error_no_data";
	public static final String ID_STR_ERROR_NEED_NET				= "lib_wei_c_msg_error_need_net";
	public static final String ID_STR_ERROR_NEED_LOGIN				= "lib_wei_c_msg_error_need_login";
	/**用户自定义错误代码配置。如：lib_wei_c_msg_error_type_123_code_456**/
	public static final String ID_STR_ERROR_USER					= "lib_wei_c_msg_error";
}
